package com.dhcc.sdc.base.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.base.entity.SdcBaseAssessmenitemEntity;
import com.dhcc.sdc.base.entity.SdcBaseAssessmentEntity;
import com.dhcc.sdc.base.service.ISdcBaseAssessmenitemService;
import com.dhcc.sdc.base.service.ISdcBaseAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 
 * 
 * @ClassName: SdcBaseAssessmenitemController
 * @Description: TODO
 * @author jumper
 * @date 2021-09-14 16:35:21
 */
@Controller
@RequestMapping("${sys.backendPath}/sdc/evaldicitem")
public class SdcBaseAssessmenitemController extends BaseController {

	private String PREFIX = "/backend/sdc/evaldicitem/";

    @Autowired
    private ISdcBaseAssessmentService sdcBaseAssessmentService;
	@Autowired
	private ISdcBaseAssessmenitemService sdcBaseAssessmenitemService;


    /**
     * 跳转到列表首页
     */
    @RequestMapping("/{asmtId}")
    public String index(@PathVariable Long asmtId,Model model) {
        List<SdcBaseAssessmentEntity> evalDic = sdcBaseAssessmentService.selectList(null);
        JSONObject asmtObj = new JSONObject();
        for(SdcBaseAssessmentEntity entity : evalDic){
            asmtObj.put(entity.getId().toString(), entity.getDescription());
        }
        model.addAttribute("asmtId", asmtId);
        model.addAttribute("asmtObj",asmtObj);

        return PREFIX + "evaldicitem.html";
    }

    /**
     * 跳转到新增
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "evaldicitem_add.html";
    }

	/**
     * 跳转到修改
     */
    @RequestMapping("/to_update/{sdcBaseAssessmenitemId}")
    public String toUpdate(@PathVariable Long sdcBaseAssessmenitemId, Model model) {
        SdcBaseAssessmenitemEntity sdcBaseAssessmenitem = this.sdcBaseAssessmenitemService.selectById(sdcBaseAssessmenitemId);
        model.addAttribute("sdcBaseAssessmenitem",sdcBaseAssessmenitem);
        return PREFIX + "evaldicitem_edit.html";
    }

    /**
     * 获取列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdcBaseAssessmenitemEntity> page = new PageFactory<SdcBaseAssessmenitemEntity>().defaultPage();
        List<SdcBaseAssessmenitemEntity> result = this.sdcBaseAssessmenitemService.page(page,params);
        page.setRecords((List<SdcBaseAssessmenitemEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增")
    public Object add(SdcBaseAssessmenitemEntity sdcBaseAssessmenitem) {
        if (CommonUtil.isOneEmpty(sdcBaseAssessmenitem)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdcBaseAssessmenitem.insert();
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除")
    public Object delete(@RequestParam Long sdcBaseAssessmenitemId) {
	
        this.sdcBaseAssessmenitemService.deleteById(sdcBaseAssessmenitemId);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改")
    public Object update(SdcBaseAssessmenitemEntity sdcBaseAssessmenitem) {
        if (CommonUtil.isOneEmpty(sdcBaseAssessmenitem)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdcBaseAssessmenitemEntity old = this.sdcBaseAssessmenitemService.selectById(sdcBaseAssessmenitem.getId());

        old.setAssessmentId(sdcBaseAssessmenitem.getAssessmentId());
        old.setCode(sdcBaseAssessmenitem.getCode());
        old.setDescription(sdcBaseAssessmenitem.getDescription());
        old.setRuleId(sdcBaseAssessmenitem.getRuleId());
        old.setMultipleChoice(sdcBaseAssessmenitem.getMultipleChoice());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
